package com.etc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.etc.dao.UserInfoDao;
import com.etc.entity.MusicInfo;
import com.etc.entity.UserInfo;
import com.etc.util.DBUtil;
/**
 * 用户Dao实现类
 * @author nxlnian
 *
 */
public class UserInfoDaoImpl implements UserInfoDao{

	@Override
	public int adduser(String userName, String password,String qq, String mobile) {
		
		String sql = "insert into user_info(user_name,mobile,qq,user_password,user_status,user_img_path) values(?,?,?,?,1,'default_avatar.jpg')";
		return DBUtil.update(sql , userName,mobile,qq,password);
	}

	@Override
	public int findQQ(String qq) {
		int i = 0;
		ResultSet rs = DBUtil.query("select count(*) from user_info where qq=?",qq);
		try {
			while (rs.next()) {
				i =  rs.getInt(1);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public int findMobile(String mobile) {
		int i = 0;
		ResultSet rs = DBUtil.query("select count(*) from user_info where mobile=?",mobile);
		try {
			while (rs.next()) {
				i =  rs.getInt(1);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public UserInfo login(String accountNumber, String password) {
		UserInfo userInfo = null;
		ResultSet rs = DBUtil.query("select user_id,user_name,user_img_path from user_info where user_password=? and  mobile = ? or qq = ? ",password,accountNumber, accountNumber);
		try {
			while (rs.next()) {
				userInfo = new UserInfo();
				userInfo.setUserId(rs.getInt("user_id"));
				userInfo.setUserName(rs.getString("user_name"));
				userInfo.setUserImgPath(rs.getString("user_img_path"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userInfo;
	}

}
